//
//  PurchaseTestViewController.m
//  PurchaseTest
//
//  Created by fanshao on 09-10-16.
//  Copyright Sensky 2009. All rights reserved.
//

#import "PurchaseTestViewController.h"

@implementation PurchaseTestViewController



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(completeTransaction:) name:@"completeTransaction" object:nil];
	//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(faliedTransaction) name:@"faliedTransaction" object:nil];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

#pragma mark -
#pragma mark SotoreKit method
#define kMyFeatureIdentifier @"witmobYacc"

- (void) requestProductData{
	NSLog(@"requestProductData");
	SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject: kMyFeatureIdentifier]];
	request.delegate = self;
	[request start];
}
#pragma mark request delegate
//!收到产品的消息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
	NSLog(@"ProductsRequest did receiveResponse");
	NSArray *myProduct = response.products;
	NSLog(@"the count of products is %d", [myProduct count]);
    SKProduct *product1;
	// populate UI
	for(SKProduct *product in myProduct){
		NSLog(@"产品的Id-->%@", [product productIdentifier]);
        NSLog(@"产品的价格-->%@",[product price]);
        NSLog(@"产品-->%@,%@,%@",[product localizedDescription],[product localizedTitle],[product priceLocale]);
        product1=product;
	}
	
//	SKPayment *payment = [SKPayment paymentWithProductIdentifier:kMyFeatureIdentifier];
   SKPayment *payment =  [SKPayment paymentWithProduct:product1];
	[[SKPaymentQueue defaultQueue] addPayment:payment];
	[request autorelease];
}
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
	UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:@"Alert" message:[error localizedDescription]
																										 delegate:nil cancelButtonTitle:NSLocalizedString(@"Close",nil) otherButtonTitles:nil];
	
	[alerView show];
	[alerView release];
}
- (void)dealloc {
    [super dealloc];
}
-(IBAction) PayForProduct{
	[self requestProductData];
}
-(IBAction)onClickButton:(id)sender
{
//    NSString* jsonObjectString = [self encode:(uint8_t *)transaction.transactionReceipt.bytes
//                                       length:transaction.transactionReceipt.length];
//    NSString* varStr = [[NSString alloc] initWithFormat:
//                        @"your_url?receipt=%@",
//                        @"dd"];
//    VMRequest* request = [[VMRequest alloc] initWithVariableStr:varStr
//                                                    requestType:PURCHASED_IAP_PRODUCT
//                                                       delegate:self];
//    [[VMRequestQueue sharedQueue] addRequest:request
//                                     toQueue:[VMRequestQueue sharedQueue].requestSetInfoQueue];
//    [mIAPRequestDict setObject:request forKey:transaction.transactionIdentifier];
//    [varStr release];
//    [request release];
    
    NSURL *sandboxStoreURL = [[NSURL alloc] initWithString: @"http://192.168.1.110:8080/EBook/image/paymoney"];
    NSMutableURLRequest *connectionRequest = [NSMutableURLRequest requestWithURL:sandboxStoreURL];
//	[connectionRequest setHTTPMethod:@"POST"];
//	[connectionRequest setTimeoutInterval:120.0];
//	[connectionRequest setCachePolicy:NSURLRequestUseProtocolCachePolicy];
//	[connectionRequest setHTTPBody:@"hello"];
    NSURLConnection *connection = [[NSURLConnection alloc]
                                   initWithRequest:connectionRequest
                                   delegate:self];
	[connection release];
    
}
#pragma mark connection delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
	NSLog(@"====>>>>>%@",  [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
	//[self.receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
	switch([(NSHTTPURLResponse *)response statusCode]) {
		case 200:
		case 206:
			break;
		case 304:
            
			break;
		case 400:
            
			break;
			
			
		case 404:
            break;
		case 416:
			break;
		case 403:
			break;
		case 401:
		case 500:
			break;
		default:
			break;
	}	
}

@end
